<?php
class model_product extends database{
	protected $_table   = "nhomsanpham";
	protected $_product = "sanpham";
	// đây là hàm thêm nhóm sản phẩm
	public function add_group($data){
		$table = $this->_table;
		$col   = implode(",",array_keys($data));
		$arr   = array_values($data);
		foreach($arr as $arr2){
			$arr3[] = "'$arr2'";
		}
		$val = implode(",",$arr3); 
		$sql = "insert into $table($col) values($val)";
		$this->query($sql);
	}
	//kiểm tra tên nhóm đã tồn tại hay chưa
	public function check_namegroup($name){
		$sql = "select tennhomsp from $this->_table where tennhomsp ='$name' ";

		$this->query($sql);
		
		if($this->num_rows() == 0){
			return TRUE;
		}else{
			return FALSE;	
		}
	}
	//lấy ra danh sách nhóm sản phẩm
	public function list_group(){
		$sql = "select * from $this->_table";
		
		$this->query($sql);

		return $this->fetchAll();
	}
	public function list_groups($start,$limit){
		$sql = "select * from $this->_table order by manhomsp DESC limit $start,$limit";
		$this->query($sql);
		return $this->fetchAll();
	}
	public function total_group(){
		$sql  = "select * from $this->_table";
		$this->query($sql);
		return $this->num_rows();
	}
	public function get_group($id){
		$sql = "select * from $this->_table where manhomsp = '$id'";
		$this->query($sql);
		return $this->fetch();
	}
	public function update_group($data,$id){
		foreach($data as $k => $v){
			$arr[] = "$k = '$v'";
		}
		$col = implode(",",$arr);
		$sql = "update $this->_table set $col where manhomsp = '$id'";
		$this->query($sql);
	}
	public function del_group($table,$id){
		$sql = "delete from $table where manhomsp = $id";
		
		$this->query($sql);
	}
	//thêm mới sản phẩm
	public function add_product($data){
		$table = $this->_product;
		$col   = implode(",",array_keys($data));
		$arr   = array_values($data);
		foreach($arr as $arr2){
			$arr3[] = "'$arr2'";
		}
		$val = implode(",",$arr3); 
		$sql = "insert into $table($col) values($val)";
		$this->query($sql);
	}
	public function total_pro(){
		$sql  = "select * from $this->_product";

		$this->query($sql);

		return $this->num_rows();
	}
	
	//
	public function list_product($start,$limit){
		$sql = "select * from $this->_product order by masanpham ASC limit $start,$limit";
		
		$this->query($sql);
		
		return $this->fetchAll();
	}
	public function list_group_product($start,$limit,$id){
		$sql = "select * from $this->_product where manhomsp = '$id' order by masanpham ASC limit $start,$limit";
		
		$this->query($sql);
		
		return $this->fetchAll();
	}
	public function total_pro_group($id){
		$sql  = "select * from $this->_product where manhomsp = '$id'";

		$this->query($sql);

		return $this->num_rows();
	}
	//xóa sản phẩm 
	public function del_product($table,$id){
		$sql = "delete from $table where masanpham = $id";
		
		$this->query($sql);
	}
	//lấy ra thông tin một sản phẩm
	public function getdata($id){
		$sql = "select * from $this->_product where masanpham = '$id'";
		
		$this->query($sql);
		
		return $this->fetch();
	}
	public function update_product($data,$id){
		foreach($data as $k => $v){
			$arr[] = "$k = '$v'";
		}
		$col = implode(",",$arr);
		$sql = "update $this->_product set $col where masanpham = '$id'";
		$this->query($sql);
	}
	//Search
	public function list_search($key,$start,$limit){
		$sql = "select * from $this->_product where tensanpham like '%$key%' limit $start,$limit";
		$this->query($sql);
		return $this->fetchAll();
	}
	public function total_search($key){
		$sql = "select * from $this->_product where tensanpham like '%$key%'";
		$this->query($sql);
		return $this->num_rows();
	}
}